package com.geekaca.news.controller.admin;

import com.geekaca.news.domain.Link;
import com.geekaca.news.service.LinkService;
import com.geekaca.news.utils.NewsConstants;
import com.geekaca.news.utils.PageResult;
import com.geekaca.news.utils.Result;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class LinkController {
    @Autowired
    private LinkService linkService;

    @RequestMapping("/links")
    public String links(HttpServletRequest request) {
        request.setAttribute("path", "links");

        return "admin/link";
    }

    @RequestMapping("/links/list")
    @ResponseBody
    public Result linkList(@Param("page") Integer page, @Param("limit") Integer limit) {
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }
        Integer totalLinks = linkService.getTotalLinks();
        List<Link> linkList = linkService.getAllLinks(page, limit);
        Result rs = new Result();
        rs.setResultCode(NewsConstants.RESULT_OK);
        PageResult pageResult = new PageResult(linkList, totalLinks, limit, page);
        rs.setData(pageResult);
        return rs;
    }

    @PostMapping("/links/save")
    @ResponseBody
    public Result linkSave(@Param("linkId") Integer linkId, @Param("linkType") Integer linkType,
                           @Param("linkName") String linkName, @Param("linkUrl") String linkUrl,
                           @Param("linkDescription") String linkDescription, @Param("linkRank") Integer linkRank) {
        System.out.println("linkType: " + linkType);
        Result res = new Result();
        Link link = new Link();
        link.setLinkId(linkId);
        link.setLinkType(linkType);
        link.setLinkName(linkName);
        link.setLinkUrl(linkUrl);
        link.setLinkDescription(linkDescription);
        link.setLinkRank(linkRank);
        link.setIsDeleted(0);
        link.setCreateTime(new Date());
        int i = linkService.insertLink(link);
        if (i > 0) {
            res.setResultCode(NewsConstants.RESULT_OK);
            res.setMessage("新增链接成功");
            res.setData(link);
        } else {
            res.setResultCode(NewsConstants.RESULT_FAIL);
            res.setMessage("新增链接失败");
        }

        return res;
    }


    @PostMapping("/links/update")
    @ResponseBody
    public Result linkUpdate(@Param("linkId") Integer linkId, @Param("linkType") Integer linkType,
                             @Param("linkName") String linkName, @Param("linkUrl") String linkUrl,
                             @Param("linkDescription") String linkDescription, @Param("linkRank") Integer linkRank) {
        System.out.println("linkType: " + linkType);
        Result res = new Result();
        Link link = new Link();
        link.setLinkId(linkId);
        link.setLinkType(linkType);
        link.setLinkName(linkName);
        link.setLinkUrl(linkUrl);
        link.setLinkDescription(linkDescription);
        link.setLinkRank(linkRank);

        int i = linkService.updateLink(link);

        if (i > 0) {
            res.setResultCode(NewsConstants.RESULT_OK);
            res.setMessage("更新链接成功");
            res.setData(link);
        } else {
            res.setResultCode(NewsConstants.RESULT_FAIL);
            res.setMessage("更新链接失败");
        }

        return res;
    }

    @PostMapping("/links/delete")
    @ResponseBody
    public Result linkDel(@RequestBody List<Long> ids){
        Result rs = new Result();
        for (Long id : ids) {
            int i = linkService.delLink(id);
            if (i>0){
                rs.setResultCode(NewsConstants.RESULT_OK);
                rs.setMessage("删除成功");
                rs.setData(ids);
            }else {
                rs.setResultCode(NewsConstants.RESULT_FAIL);
                rs.setMessage("删除失败");
                break;
            }
        }
        return rs;
    }

    @RequestMapping("/links/info/{id}")
    @ResponseBody
    public Result linkInfo(@PathVariable("id") Long id) {

        Link link = linkService.getLinkById(id);
        Result result = new Result();
        if (link != null) {
            result.setResultCode(NewsConstants.RESULT_OK);
            result.setMessage("查找成功");
            result.setData(link);
        } else {
            result.setResultCode(NewsConstants.RESULT_FAIL);
            result.setMessage("查找失败");
        }
        return result;
    }
}
